package com.wiz.weixin.service.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.apache.http.NameValuePair;
import org.apache.http.message.BasicNameValuePair;
import org.json.JSONArray;
import org.json.JSONObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import com.google.gson.Gson;
import com.wiz.weixin.common.Constant;
import com.wiz.weixin.manager.HttpManager;
import com.wiz.weixin.mapper.StationMapper;
import com.wiz.weixin.service.StationService;
import com.wiz.weixin.to.StationTO;
import com.wiz.weixin.util.PageView;

@Service("stationService")
public class StationServiceImpl implements StationService {
	private transient final Logger log = LoggerFactory.getLogger(getClass());

	@Resource
	private Gson gson;
	@Resource
	private StationMapper stationMapper;
	@Resource
	private HttpManager httpManager;

	@Override
	public String getNearbyStationsByHqIds(List<String>hqIds,double lat,double lng) {
		log.info("hqIds="+hqIds.toString()+"  getNearbyStationsByHqIds by lng="+lng+"   lat="+lat);
		PageView pageView = new PageView(10, 1);
		Map<String, Object>map = new HashMap<String, Object>();
		map.put("paging", pageView);
		map.put("hqIds",hqIds );
		map.put("lat",lat);
		map.put("lng",lng);
		map.put("pi","3.14159265358979323846");
		List<StationTO> list = stationMapper.getNearbyStationsByHqIds(map);
		pageView.setRecords(list);
		pageView.setCode(200);
		return gson.toJson(pageView);
	}
	
	@Override
	public List<String> getHQIds(String openId) throws Exception{
		List<String> hqIds = new ArrayList<String>();
		List<NameValuePair> formparams = new ArrayList<NameValuePair>();
		formparams.add(new BasicNameValuePair("openId", openId));
		String result = httpManager.getResponseByHttpPost(Constant.QUERY_MEMBER_URL, formparams);
		log.info(openId+" getHQIds response="+result+"   params="+formparams.toString());
		JSONObject json = new JSONObject(result);
		if(!json.isNull("code")){
			String codes = json.getString("code");
			if(codes.equals("0")){
				JSONObject data = json.getJSONObject("data");
				JSONArray list = data.getJSONArray("list");
				for(int i = 0;i<list.length();i++){
					hqIds.add(list.getJSONObject(i).getString("HQId"));
				}
			}
		}
       return hqIds;
	}
	
	
	@Override
	public String getHQId(String openId,String hqId) throws Exception{
		List<NameValuePair> formparams = new ArrayList<NameValuePair>();
		formparams.add(new BasicNameValuePair("openId", openId));
		formparams.add(new BasicNameValuePair("HQId", hqId));
		String result = httpManager.getResponseByHttpPost(Constant.QUERY_MEMBER_URL, formparams);
		log.info(openId+" getHQIds response="+result+"   params="+formparams.toString());
		JSONObject json = new JSONObject(result);
		if(!json.isNull("code")){
			String codes = json.getString("code");
			if(codes.equals("0")){
				JSONObject data = json.getJSONObject("data");
				if(data.getInt("recordCnt")>0){
					JSONArray list = data.getJSONArray("list");
					return list.getJSONObject(0).getString("HQId");
				}
			}
		}
       return null;
	}

	@Override
	public StationTO getStationById(String stationId) {
		return stationMapper.getStationById(stationId);
	}
}
